/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.example.cim.controller;

import com.example.cim.init.ServerCache;
import com.example.cim.model.CIMSession;
import com.example.cim.model.Message;
import com.example.cim.model.ServerInfo;
import com.example.cim.push.DefaultMessagePusher;
import com.example.cim.repository.SessionRepository;
import com.example.cim.route.RouteHandle;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@RestController
@Api(value = "message", description = "消息管理页面")
@RequestMapping("/api/message")
public class MessageController {

    @Resource
    private DefaultMessagePusher defaultMessagePusher;
    @Autowired
    private RouteHandle routeHandle ;
    @Autowired
    private ServerCache serverCache;
    @Autowired
    private SessionRepository sessionRepository;

    @PostMapping(value = "/send")
    public ResponseEntity<Long> send(Message message) {
        message.setId(System.currentTimeMillis());
        defaultMessagePusher.push(message);
        return ResponseEntity.ok(message.getId());
    }

    @PostMapping(value = "/online/get")
    public ResponseEntity<List<CIMSession>> getOnline() {
        return ResponseEntity.ok(sessionRepository.findAll());
    }

    @PostMapping(value = "/server/get")
    public ResponseEntity<ServerInfo> get() {
        String server = routeHandle.routeServer(serverCache.getServerList(),"1");
        String[] split = server.split(":");
        ServerInfo serverInfo = new ServerInfo(split[0], Integer.parseInt(split[1]), Integer.parseInt(split[2]), Integer.parseInt(split[3]));
        return ResponseEntity.ok(serverInfo);
    }

}
